package com.name.givename.serviceimpl;

import java.util.Random;

import com.name.givename.enums.ResultEnum;
import com.name.givename.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.name.givename.dao.UserBonusInfoEntityMapper;
import com.name.givename.dao.UserInfoEntityMapper;
import com.name.givename.entity.UserBonusInfoEntity;
import com.name.givename.entity.UserInfoEntity;
import com.name.givename.entity.UserInformationEntity;
import com.name.givename.param.BindPhoneParams;
import com.name.givename.param.PhoneVerifyCodeParam;
import com.name.givename.service.UserInfoService;
import com.name.givename.utils.SmsUntil;
import com.name.givename.utils.UUIDUtils;

/**
 * Created by yangtianrui on 18/9/2.
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired private UserInfoEntityMapper userInfoEntityMapper;

    @Autowired private UserBonusInfoEntityMapper userBonusInfoEntityMapper;

    @Override
    public UserInfoEntity getUserInfo(Integer userId) {
        return userInfoEntityMapper.selectByPrimaryKey(userId);
    }

    @Override
    public UserInformationEntity getUserInformation(String openId) {
        UserInformationEntity userInformationEntity = new UserInformationEntity();
        // 用户提现金额
        UserBonusInfoEntity userBonusInfoEntity = userBonusInfoEntityMapper.getUserWinMoney(openId);
        userInformationEntity = userInfoEntityMapper.getUserInformationByOpenId(openId);
        userInformationEntity.setWinMoney(userBonusInfoEntity.getWinMoney());
        return userInformationEntity;
    }

    @Override
    public Integer bindPhone(BindPhoneParams bindPhoneParams) {
        // 根据用户openid查询出该用户是否已经绑定过
        UserInfoEntity instance = userInfoEntityMapper.selectByUserPhone(bindPhoneParams.getPhone());
        if (instance != null && instance.getBindStatus().equals("1")) {
            // 该用户已经绑定
            throw new ServiceException(ResultEnum.USER_BINDED_ALREADY.getCode(),ResultEnum.USER_BINDED_ALREADY.getValue());
        }
        //校验验证码
        if (!bindPhoneParams.getSmsCode().equals(instance.getPhoneCode())){
            throw new ServiceException(ResultEnum.SMSCODE_WORNG.getCode(),ResultEnum.SMSCODE_WORNG.getValue());
        }
        return userInfoEntityMapper.bindPhone(bindPhoneParams);
    }
    
    @Override
    public SendSmsResponse sendVerifyCode(PhoneVerifyCodeParam phoneVerifyCodeParam) {
    	SendSmsResponse response  = null;
    	//生成短信验证码 6位
    	String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
    	//业务流水号
    	String UUID = UUIDUtils.getUUID();
    	System.out.println("短信验证码为:"+verifyCode+",业务流水号为："+UUID);
    	try {
    		 response = SmsUntil.sendSms(phoneVerifyCodeParam.getPhone(), verifyCode, UUID);
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return response;
    }
}
